package com.stary.springmvc.web.fileUpload;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 处理文件上传的handler
 * @author: Guan
 **/
@Controller
public class FileUploadHandler {
    /**
     * 编写方法，处理文件上传
     * @return
     */
    @RequestMapping("/fileUpload")
    public String fileUpload(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request,
                             String introduce) throws IOException {
        //接收提交的文件名字
        String originalFilename = file.getOriginalFilename();
        System.out.println("接收到的文件名 = " + originalFilename);
        System.out.println("introduce = " + introduce);
        //将接收的文件保存哪个路径上
        String path = request.getServletContext().getRealPath("/img/" + originalFilename);
        //创建文件
        File saveToFile = new File(path);
        //将接收的文件保存到路径上
        file.transferTo(saveToFile);
        return "success";
    }
}
